package uit.minito.views;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;

public class Utility {


	public static int geoToInt(String geo) {
		Double dgeo = Double.parseDouble(geo);
		dgeo = dgeo * 1E6;  
		DecimalFormat df = new DecimalFormat("#");
		String rs = df.format(dgeo);

		int v = Integer.valueOf(rs);
		return v;
	}
	
	
	public static String getJsonFromURL (String url) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response;
		String responseString = null;
		try {
			response = httpclient.execute(new HttpGet(url)); //url[0]
			StatusLine statusLine = response.getStatusLine();
			if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				response.getEntity().writeTo(out);
				out.close();
				responseString = out.toString();

			} else {
				response.getEntity().getContent().close();
				throw new IOException(statusLine.getReasonPhrase());
			}
		} catch (ClientProtocolException e) {

		} catch (IOException e) {

		}
		return responseString;
	}
	
	public static void zoomInBounds(final GoogleMap map, final Vector<LatLng> points) {
		LatLngBounds.Builder bld = new LatLngBounds.Builder();

	    for (LatLng point : points) {
	    	bld.include(new LatLng(point.latitude, point.longitude));
	    }
	    
	    LatLngBounds bounds = bld.build();
	    map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
	}
	
}
